{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #最小展台数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minNumBooths"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最小展台数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "力扣嘉年华将举办一系列展览活动，后勤部将负责为每场展览提供所需要的展台。\n",
    "已知后勤部得到了一份需求清单，记录了近期展览所需要的展台类型， `demand[i][j]` 表示第 `i` 天展览时第 `j` 个展台的类型。\n",
    "在满足每一天展台需求的基础上，请返回后勤部需要准备的 **最小** 展台数量。\n",
    "\n",
    "**注意：**\n",
    "- 同一展台在不同天中可以重复使用。\n",
    "\n",
    "**示例 1：**\n",
    ">输入：`demand = [\"acd\",\"bed\",\"accd\"]`\n",
    ">\n",
    ">输出：`6`\n",
    ">\n",
    ">解释：\n",
    ">第 `0` 天需要展台 `a、c、d`；\n",
    ">第 `1` 天需要展台 `b、e、d`；\n",
    ">第 `2` 天需要展台 `a、c、c、d`；\n",
    ">因此，后勤部准备 `abccde` 的展台，可以满足每天的展览需求;\n",
    "\n",
    "**示例 2：**\n",
    ">输入：`demand = [\"abc\",\"ab\",\"ac\",\"b\"]`\n",
    ">\n",
    ">输出：`3`\n",
    "\n",
    "\n",
    "**提示：**\n",
    "- `1 <= demand.length,demand[i].length <= 100`\n",
    "- `demand[i][j]` 仅为小写字母"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [600YaG](https://leetcode.cn/problems/600YaG/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [600YaG](https://leetcode.cn/problems/600YaG/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"acd\",\"bed\",\"accd\"]', '[\"abc\",\"ab\",\"ac\",\"b\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        from collections import Counter\n",
    "        num = 0\n",
    "        dct = {}\n",
    "        for ds in demand:\n",
    "            freq = Counter(ds)\n",
    "            for k, v in freq.items():\n",
    "                if k not in dct:\n",
    "                    dct[k] = v\n",
    "                else:\n",
    "                    dct[k] = max(dct[k], v) \n",
    "        return sum(dct.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dic = {}\n",
    "        for item in demand:\n",
    "            dic2 = {}\n",
    "            for ch in item:\n",
    "                if ch not in dic and ch not in dic2:\n",
    "                    dic[ch] = 1\n",
    "                    dic2[ch] = 1\n",
    "                elif ch not in dic and ch in dic2:  # no\n",
    "                    pass\n",
    "                elif ch in dic and ch not in dic2:\n",
    "                    dic2[ch] = 1\n",
    "                elif ch in dic and ch in dic2:\n",
    "                    dic2[ch] += 1\n",
    "                    if dic2[ch] > dic[ch]:\n",
    "                        dic[ch] = dic2[ch]\n",
    "        # print(dic)\n",
    "        res = 0\n",
    "        for v in dic.values():\n",
    "            res += v\n",
    "        return res\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        mp = Counter()\n",
    "        for s in demand:\n",
    "            c = Counter(s)\n",
    "            for k, v in c.items():\n",
    "                if k not in mp:\n",
    "                    mp[k] = v\n",
    "                else:\n",
    "                    mp[k] = max(v, mp[k])\n",
    "        return sum(mp.values())\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        cnt = defaultdict(int)\n",
    "        for d in demand:\n",
    "            c = Counter(d)\n",
    "            for k in c:\n",
    "                cnt[k]=max(cnt[k],c[k])\n",
    "        return sum(cnt.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        alphabet = defaultdict(int)\n",
    "        for s in demand:\n",
    "            t = defaultdict(int)\n",
    "            for ch in s:\n",
    "                t[ch] += 1\n",
    "                alphabet[ch] = max(alphabet[ch], t[ch])\n",
    "        return sum(alphabet.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        x = 0\n",
    "        c = {}\n",
    "        for i in demand:\n",
    "            k = Counter(i)\n",
    "            for j in k:\n",
    "                if c.get(j):\n",
    "                    if k[j] > c[j]:\n",
    "                        x += (k[j]-c[j])\n",
    "                        c[j] = k[j]\n",
    "                else:\n",
    "                    c[j] = k[j]\n",
    "                    x += k[j]\n",
    "        return x\n",
    "                         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        res = defaultdict(int)\n",
    "        for row in demand:\n",
    "            for ch, c in Counter(row).items():\n",
    "                res[ch] = max(res[ch], c)\n",
    "        return sum(res.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        dic = {}\n",
    "        res = 0\n",
    "        for char in demand:\n",
    "            for c in char:\n",
    "                if c in dic:\n",
    "                    dic[c] = max(char.count(c),dic[c])\n",
    "                else:\n",
    "                    dic[c] = char.count(c)\n",
    "        for i in dic:\n",
    "            res+=dic[i]\n",
    "        return res\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        # print(collections.Counter(a))\n",
    "        tmp = dict()\n",
    "        for i in demand:\n",
    "            tmp_dict = dict(collections.Counter(i))\n",
    "            for i in tmp_dict:\n",
    "                if i in tmp and tmp_dict[i] >= tmp[i]:\n",
    "                    tmp[i] = tmp_dict[i]\n",
    "                elif i in tmp and tmp_dict[i] <= tmp[i]:\n",
    "                    # 如果当前没大于之前的次数，则忽略\n",
    "                    continue\n",
    "                else:\n",
    "                    tmp[i] = tmp_dict[i]\n",
    "            \n",
    "        return sum(tmp.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        d=collections.Counter(demand[0])\n",
    "        for i in range(1,len(demand)):\n",
    "            s=d|collections.Counter(demand[i])\n",
    "            d=s\n",
    "        return sum(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        res = defaultdict(int)\n",
    "        for row in demand:\n",
    "            for ch, c in Counter(row).items():\n",
    "                res[ch] = max(res[ch], c)\n",
    "        return sum(res.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        xf = [0 for _ in range(26)]\n",
    "        for s in demand:\n",
    "            cur = [0 for _ in range(26)]\n",
    "            for c in s:\n",
    "                ID = ord(c) - ord('a')\n",
    "                cur[ID] += 1\n",
    "            for x in range(26):\n",
    "                xf[x] = max(xf[x], cur[x])\n",
    "        res = sum(xf)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        s = ''.join(demand)\n",
    "        dup_s = list(set(list(s)))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        ans = Counter()\n",
    "        for d in demand:\n",
    "            temp = Counter(d)\n",
    "            for key in temp.keys():\n",
    "                ans[key] = ans[key] if ans[key] >= temp[key] else temp[key]\n",
    "        return sum(ans.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def minNumBooths(self, demand: List[str]) -> int:\n",
    "    mapping = defaultdict(int)\n",
    "    for day in demand:\n",
    "      for k, v in Counter(day).items():\n",
    "        mapping[k] = max(mapping[k], v)\n",
    "    return sum(mapping.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        d0 = {}\n",
    "        for i in range(len(demand)):\n",
    "            d = {}\n",
    "            for c in demand[i]:\n",
    "                if not c in d:\n",
    "                    d[c] = 1\n",
    "                else:\n",
    "                    d[c] += 1\n",
    "            for c in d:\n",
    "                count = d[c]\n",
    "                if not c in d0 or d0[c] < count:\n",
    "                    d0[c] = count\n",
    "        return sum(d0.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        ans=\"\"\n",
    "        for i in demand:\n",
    "            for j in i:\n",
    "                if j not in ans or i.count(j)>ans.count(j):\n",
    "                    ans+=j\n",
    "        return len(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        res = Counter()\n",
    "        for d in demand:\n",
    "            tmp = Counter(d)\n",
    "            res = res | tmp\n",
    "        return res.total()\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_,map(Counter,demand)).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        xf = [0 for _ in range(26)]\n",
    "        for s in demand:\n",
    "            cur = [0 for _ in range(26)]\n",
    "            for c in s:\n",
    "                ID = ord(c) - ord('a')\n",
    "                cur[ID] += 1\n",
    "            for x in range(26):\n",
    "                xf[x] = max(xf[x], cur[x])\n",
    "        \n",
    "        res = sum(xf)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        from collections import Counter\n",
    "        num = 0\n",
    "        dct = {}\n",
    "        for ds in demand:\n",
    "            freq = Counter(ds)\n",
    "            for k, v in freq.items():\n",
    "                if k not in dct:\n",
    "                    dct[k] = v\n",
    "                else:\n",
    "                    dct[k] = max(dct[k], v) \n",
    "        return sum(dct.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        c=Counter(demand[0])\n",
    "        for i in demand:\n",
    "            c=Counter(i)|c\n",
    "        return sum(c.values())\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        p = dict([])\n",
    "        for i in demand:\n",
    "            day = Counter(i)\n",
    "            for d in day:\n",
    "                if d not in p:\n",
    "                    p[d] = day[d]\n",
    "                else:\n",
    "                    p[d] = max(p[d], day[d])\n",
    "        return sum(p.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for i in demand:\n",
    "            c = Counter(i)\n",
    "            res.append(c)\n",
    "\n",
    "        d = {}\n",
    "\n",
    "        for j in res:\n",
    "            for k in j:\n",
    "                if k not in d:\n",
    "                    d[k] = j[k]\n",
    "                elif j[k] > d[k]:\n",
    "                    d[k] = j[k]\n",
    "\n",
    "        return sum(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        cnt = dict()\n",
    "        for item in demand:\n",
    "            counter = Counter(item)\n",
    "            for k,v in counter.items():\n",
    "                if k not in cnt or cnt[k]<v:\n",
    "                    cnt[k]=v\n",
    "        return sum(cnt.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        char = \"qwertyuiopasdfghjklzxcvbnm\"\n",
    "        limit = {char[i]: 0 for i in range(len(char))}\n",
    "        for d in demand:\n",
    "            count = {char[i]: 0 for i in range(len(char))}\n",
    "            for j in d:\n",
    "                count[j] += 1\n",
    "            for k in d:\n",
    "                if limit[k] < count[k]:\n",
    "                    limit[k] = count[k]\n",
    "\n",
    "        return sum(limit.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        prepare = [0] * 26\n",
    "        alpbt = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        for day in demand:\n",
    "            for i in range(0,26):\n",
    "                prepare[i] = max(prepare[i], day.count(alpbt[i]))\n",
    "        result = ''\n",
    "        for stage in range(0,26):\n",
    "            result += alpbt[stage] * prepare[stage]\n",
    "        return len(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        # 遍历每个字符串 得到字母出现次数 要是大于之前的最大值就更新\n",
    "        use = []\n",
    "        ans = []\n",
    "        for i in range(27):\n",
    "            use.append(0)\n",
    "            ans.append(0)\n",
    "\n",
    "        for i in demand:\n",
    "            for j in range(27):\n",
    "                use[j] = 0\n",
    "            # 每遍历一次字符串就清空一次use数组   \n",
    "            for j in i:\n",
    "                use[ord(j)-ord('a')] += 1\n",
    "            \n",
    "            for j in range(27):\n",
    "                if use[j] > ans[j]:ans[j] = use[j]\n",
    "\n",
    "        return sum(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dic = {}\n",
    "        res = 0\n",
    "        for char in demand:\n",
    "            for c in char:\n",
    "                if c in dic:\n",
    "                    dic[c] = max(char.count(c),dic[c])\n",
    "                else:\n",
    "                    dic[c] = char.count(c)\n",
    "        for i in dic:\n",
    "            res+=dic[i]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for i in demand:\n",
    "            c = Counter(i)\n",
    "            res.append(c)\n",
    "\n",
    "        d = {}\n",
    "\n",
    "        for j in res:\n",
    "            for k in j:\n",
    "                if k not in d:\n",
    "                    d[k] = j[k]\n",
    "                elif j[k] > d[k]:\n",
    "                    d[k] = j[k]\n",
    "\n",
    "        return sum(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        booths = [0] * 26\n",
    "        for d in demand:\n",
    "            m = Counter(d)\n",
    "            for key, value in m.items():\n",
    "                index = ord(key) - ord('a')\n",
    "                booths[index] = max(booths[index], value)\n",
    "        return sum(booths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        l=[0]*26\n",
    "        for i in demand:\n",
    "            cnt=Counter(i)\n",
    "            for j in cnt:\n",
    "                if l[ord(j)-ord('a')]<cnt[j]:\n",
    "                    l[ord(j)-ord('a')]=cnt[j]\n",
    "        return sum(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        booths = [0] * 26\n",
    "        for d in demand:\n",
    "            m = defaultdict(int)\n",
    "            for s in d:\n",
    "                m[s] += 1\n",
    "            for key, value in m.items():\n",
    "                index = ord(key) - ord('a')\n",
    "                booths[index] = max(booths[index], value)\n",
    "        return sum(booths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        res = Counter()\n",
    "        for d in demand:\n",
    "            tmp = Counter(d)\n",
    "            for key in tmp.keys():\n",
    "                res[key] = res[key] if res[key] >= tmp[key] else tmp[key]\n",
    "        return sum(res.values())\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        hax = {}\n",
    "        for x in demand:\n",
    "            z = 1\n",
    "            for y in x:\n",
    "                if y not in hax:\n",
    "                    hax[y] = 1\n",
    "                else:\n",
    "                    hax[y] = max(hax[y], x.count(y)) \n",
    "        return sum(hax.values())\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        zt = {}\n",
    "        for i in range(len(demand)):\n",
    "            cnt = {}\n",
    "            for c in demand[i]:\n",
    "                if c in cnt.keys():\n",
    "                    cnt[c]+=1\n",
    "                else:\n",
    "                    cnt[c]=1\n",
    "            for x in cnt.keys():\n",
    "                if x in zt.keys():\n",
    "                    zt[x] = max(zt[x], cnt[x])\n",
    "                else:\n",
    "                    zt[x]=cnt[x]\n",
    "\n",
    "        s=sum(zt.values())\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        # demand[i][j] 表示第 i 天展览时第 j 个展台的类型\n",
    "        # 如果是同一天相同的话需要算2个\n",
    "        results = {}\n",
    "        for dd in demand:\n",
    "            for d in set(dd):\n",
    "                if d not in results or results[d] < dd.count(d):\n",
    "                    # 更新字典\n",
    "                    results[d] = dd.count(d)\n",
    "        last_result = 0\n",
    "        for value in results.values():\n",
    "            last_result += value\n",
    "        return last_result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return reduce(or_, map(Counter, demand)).total()\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        res = Counter()\n",
    "        for d in demand:\n",
    "            tmp = Counter(d)\n",
    "            for key in tmp.keys():\n",
    "                res[key] = res[key] if res[key] >= tmp[key] else tmp[key]\n",
    "        return sum(res.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        allc = Counter()\n",
    "        for d in demand:\n",
    "            c = Counter(d)\n",
    "            for k in c:\n",
    "                allc[k] = max(allc[k], c[k])\n",
    "        res = 0\n",
    "        for x in allc:\n",
    "            res += allc[x]\n",
    "        return res                            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        ans = [0] * 26\n",
    "        for d in demand:\n",
    "            cnt = Counter(d)\n",
    "            for k, v in cnt.items():\n",
    "                i = ord(k) - ord('a')\n",
    "                ans[i] = max(ans[i], v)\n",
    "        return sum(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        booths = [0] * 26\n",
    "        for d in demand:\n",
    "            m = defaultdict(int)\n",
    "            for s in d:\n",
    "                m[s] += 1\n",
    "            for key, value in m.items():\n",
    "                index = ord(key) - ord('a')\n",
    "                booths[index] = max(booths[index], value)\n",
    "        return sum(booths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())\n",
    "        \n",
    "\n",
    "        \n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        total = defaultdict(int)\n",
    "        for d in demand:\n",
    "            day = defaultdict(int)\n",
    "            for i in range(len(d)):\n",
    "                day[d[i]] += 1\n",
    "            for key in day:\n",
    "                if day[key] > total[key]:\n",
    "                    total[key] = day[key]\n",
    "        ans = 0\n",
    "        for key in total:\n",
    "            ans += total[key]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        demandDict = {}\n",
    "        for data in demand:\n",
    "            thisDict = {}\n",
    "            for b in data:\n",
    "                if b in thisDict:\n",
    "                    thisDict[b]+=1\n",
    "                else:\n",
    "                    thisDict[b] = 1\n",
    "            for this in thisDict:\n",
    "                if (this in demandDict and demandDict[this] < thisDict[this]) or this not in demandDict:\n",
    "                    demandDict[this] = thisDict[this]\n",
    "        \n",
    "        return sum(demandDict[key] for key in demandDict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        res = Counter()\n",
    "        for d in demand:\n",
    "            tmp = Counter(d)\n",
    "            res = res | tmp\n",
    "        return sum(res.values())\n",
    "                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dic={}\n",
    "        res=0\n",
    "        for day in demand:\n",
    "            for ch in day:\n",
    "                if ch in dic:\n",
    "                    # ch-某个展台，如果这个展台在 dic里，那就看今天这个展台需要的数量，和 dic里已有的数量，取最大值进行更新\n",
    "                    dic[ch]=max(day.count(ch),dic[ch])\n",
    "                else:\n",
    "                    # 如果是新添加的展台，就按照当天的需求数量进行增加\n",
    "                    dic[ch]=day.count(ch)\n",
    "        # 所有的展台数量\n",
    "        nums=[value for value in dic.values()]\n",
    "        return sum(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        d = dict()\n",
    "        for str in demand:\n",
    "            for ch in str:\n",
    "                if ch not in d:\n",
    "                    d[ch] = str.count(ch)\n",
    "                else:\n",
    "                    d[ch] = max(d[ch], str.count(ch))\n",
    "        return sum(d.values())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        demand = [Counter(i) for i in demand]\n",
    "        c = Counter()\n",
    "        for i in demand:\n",
    "            for j in i:\n",
    "                c[j] = max(c[j], i[j])\n",
    "        return sum(i for i in c.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        flag = 0\n",
    "        counter = set(''.join(demand))\n",
    "        dc = {}\n",
    "        for words in demand:\n",
    "            char_counter = Counter(words).items()\n",
    "            for key, value in char_counter:\n",
    "                if value != 1:\n",
    "                    if value > dc.get(key, 0):\n",
    "                        dc[key] = value\n",
    "\n",
    "        for value in dc.values():\n",
    "            flag += value - 1\n",
    "       \n",
    "        return len(counter) + flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### my：数组+遍历\n",
    "# class Solution:\n",
    "#     def minNumBooths(self, demand: List[str]) -> int:\n",
    "#         pre=[0]*26\n",
    "#         for d in demand:\n",
    "#             cur=[0]*26\n",
    "#             for ch in d:\n",
    "#                 cur[ord(ch)-ord(\"a\")]+=1\n",
    "#             for i in range(26):\n",
    "#                 cur[i]=max(cur[i],pre[i])\n",
    "#             pre=cur\n",
    "#         return sum(pre)\n",
    "\n",
    "### 灵茶山：\n",
    "class Solution:\n",
    "\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        return sum(reduce(or_, map(Counter, demand)).values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        count = {}\n",
    "        for i in demand:\n",
    "            t = Counter(i)\n",
    "            for k, v in t.items():\n",
    "                if k not in count.keys():\n",
    "                    count[k] = v\n",
    "                else:\n",
    "                    if count[k] < v:\n",
    "                        count[k] = v\n",
    "        return sum(count.values())\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dp = [0]*26\n",
    "        m = len(demand)\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            tmp = [0]*26\n",
    "            res = 0\n",
    "            for c in demand[i]:\n",
    "                num = ord(c) - ord('a')\n",
    "                tmp[num] += 1\n",
    "            for j in range(26):\n",
    "                dp[j] = max(dp[j],tmp[j])\n",
    "                res += dp[j]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        s = ''.join(demand)\n",
    "        dup_s = list(set(list(s)))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        counter = set(''.join(demand))\n",
    "        dc = {}\n",
    "        for words in demand:\n",
    "            char_counter = Counter(words).items()\n",
    "            for key, value in char_counter:\n",
    "                if value != 1:\n",
    "                    if value > dc.get(key, 0):\n",
    "                        dc[key] = value\n",
    "\n",
    "        flag = sum(dc.values(), -len(dc))\n",
    "       \n",
    "        return len(counter) + flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        counter = set(''.join(demand))\n",
    "        dc = {}\n",
    "        for words in demand:\n",
    "            char_counter = Counter(words).items()\n",
    "            for key, value in char_counter:\n",
    "                if value != 1:\n",
    "                    if value > dc.get(key, 0):\n",
    "                        dc[key] = value\n",
    "\n",
    "        flag = sum(dc.values()) - len(dc)\n",
    "       \n",
    "        return len(counter) + flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "        res = []\n",
    "\n",
    "        for i in demand:\n",
    "            c = Counter(i)\n",
    "            res.append(c)\n",
    "\n",
    "        d = {}\n",
    "\n",
    "        for j in res:\n",
    "            for k in j:\n",
    "                if k not in d:\n",
    "                    d[k] = j[k]\n",
    "                elif j[k] > d[k]:\n",
    "                    d[k] = j[k]\n",
    "\n",
    "        return sum(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        return sum(max(x.count(c) for x in demand) for c in ascii_lowercase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dict_res = {}\n",
    "        for i in range(len(demand)):\n",
    "            dict_a = {}\n",
    "            for j in demand[i]:\n",
    "                if j not in dict_a.keys():\n",
    "                    dict_a[j] = 1\n",
    "                else:\n",
    "                    dict_a[j] += 1\n",
    "            \n",
    "            for k in dict_a.keys():\n",
    "                if k not in dict_res.keys():\n",
    "                    dict_res[k] = dict_a[k]\n",
    "                else:\n",
    "                    if dict_a[k] > dict_res[k]:\n",
    "                        dict_res[k] = dict_a[k]\n",
    "            \n",
    "        res = 0\n",
    "        for i in dict_res.keys():\n",
    "            res += dict_res[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "\n",
    "\n",
    "        main_dict = collections.defaultdict(int)\n",
    "\n",
    "        for dem in demand:\n",
    "            today_dict = collections.defaultdict(int)\n",
    "\n",
    "            for ch in dem:\n",
    "                if ch not in today_dict:\n",
    "                    today_dict[ch] += 1\n",
    "                    if ch not in main_dict:\n",
    "                        main_dict[ch] += 1\n",
    "                else:\n",
    "                    # already existed\n",
    "                    today_dict[ch] += 1\n",
    "                    if today_dict[ch] <= main_dict[ch]:\n",
    "                        continue\n",
    "                    else:\n",
    "                        main_dict[ch] += 1\n",
    "        \n",
    "\n",
    "        ans = 0\n",
    "        for key, value in main_dict.items():\n",
    "            ans += value\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minNumBooths(self, demand: List[str]) -> int:\n",
    "        dup_s = list(set(list(''.join(demand))))\n",
    "        num = 0\n",
    "        for i in dup_s:\n",
    "            num1 = 0\n",
    "            for j in demand:\n",
    "                if j.count(i) > num1:\n",
    "                    num1 = j.count(i)\n",
    "            num += num1\n",
    "        return num\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
